using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace BTL.Technology.Data
{
    public class DataHelper : IDataHelper
    {

        #region IDataHelper Members


        public TObject FillObject<TObject>(string connectionString, string procedureName, params object[] parameterValues)
        {
            return FillObject<TObject>(connectionString, procedureName, true, parameterValues);
        }

        public TObject FillObject<TObject>(string connectionString, string procedureName, bool closeReader, params object[] parameterValues)
        {
            var dataReader = SqlHelper.ExecuteReader(
                connectionString,
                procedureName,
                parameterValues);

            var retValue = ObjectHelper.FillObject<TObject>(dataReader, closeReader);

            return retValue;
        }
        

        public List<TItem> FillCollection<TItem>(string connectionString, string procedureName, params object[] parameterValues)
        {
            return FillCollection<TItem>(connectionString, procedureName, true, parameterValues);
        }

        public List<TItem> FillCollection<TItem>(string connectionString, string procedureName, bool closeReader, params object[] parameterValues)
        {
            var dataReader = SqlHelper.ExecuteReader(
                connectionString,
                procedureName,
                parameterValues);

            var retValue = ObjectHelper.FillCollection<TItem>(dataReader, closeReader);

            return retValue;
        }

        
        public DataSet ExecuteDataset(string connectionString, string procedureName, params object[] parameterValues)
        {
            var dataSet = SqlHelper.ExecuteDataset(
                connectionString,
                procedureName,
                parameterValues);

            return dataSet;
        }

        public SqlDataReader ExecuteReader(string connectionString, string procedureName, params object[] parameterValues)
        {
            var dataSet = SqlHelper.ExecuteReader(
                connectionString,
                procedureName,
                parameterValues);

            return dataSet;
        }

        public TReturn ExecuteScalar<TReturn>(string connectionString, string spName, params object[] parameterValues)
        {
            var scalar = SqlHelper.ExecuteScalar(
                connectionString,
                spName,
                parameterValues);

            if (scalar == null) return default(TReturn);


            //TODO: find a w
            try
            {
                var retValue = Convert.ChangeType(scalar, typeof (TReturn));

                return (TReturn) retValue;  
            }
            catch
            {

                return (TReturn)scalar;
            }

        }

        public void ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
        {
            SqlHelper.ExecuteScalar(
                connectionString,
                spName,
                parameterValues);

        }

        #endregion



        #region IDataHelper Members


        public string SerializeObject(object objObject)
        {
            return ObjectHelper.SerializeObject(objObject);
        }

        public string SerializeObject(object objObject, string root)
        {
            return ObjectHelper.SerializeObject(objObject, root);
        }

        public string SerializeCollection(object objCollection)
        {
            return ObjectHelper.SerializeCollection(objCollection);
        }

        public string SerializeCollection(object objCollection, string root)
        {
            return ObjectHelper.SerializeCollection(objCollection, root);
        }

        public string SerializeCollection(object objCollection, string root, System.Text.Encoding encoding)
        {
            return ObjectHelper.SerializeCollection(objCollection, root, encoding);
        }

        #endregion
    }
}